﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml;
using System.Xml.Serialization;


namespace SusyGame_Model
{
    [Serializable]
    public class Wave
    {
        private int numOfEnemies; //Cantidad de enemigos a aparecer
        private int waveNumber;
        private float spawnTimer = 0; //Cuando se crea un enemigo
        private int enemiesSpawned = 0; //Cantidad de enemigos que ya han aparecido
        private bool enemyAtEnd; //Verifica si el enemigo llega al final del camino
        private bool spawningEnemies; //Verificara si aun se siguen creando enemigos
        private Map map;
        private Texture2D enemyTexture;
        private List<Enemy> enemies = new List<Enemy>();

        public int NumOfEnemies
        {
            get { return numOfEnemies; }
            set { numOfEnemies = value; }
        }
        
        public int WaveNumber
        {
            get { return waveNumber; }
            set { waveNumber = value; }
        }
        
        public float SpawnTimer
        {
            get { return spawnTimer; }
            set { spawnTimer = value; }
        }
        
        public int EnemiesSpawned
        {
            get { return enemiesSpawned; }
            set { enemiesSpawned = value; }
        }       

        public bool EnemyAtEnd
        {
            get { return enemyAtEnd; }
            set { enemyAtEnd = value; }
        }       

        public bool SpawningEnemies
        {
            get { return spawningEnemies; }
            set { spawningEnemies = value; }
        }
        
        public Map Map
        {
            get { return map; }
            set { map = value; }
        }        

        public Texture2D EnemyTexture
        {
            get { return enemyTexture; }
            set { enemyTexture = value; }
        }       

        public List<Enemy> Enemies
        {
            get { return enemies; }
            set { enemies = value; }
        }

        public bool RoundOver
        {
            get
            {
                return enemies.Count == 0 && enemiesSpawned == numOfEnemies;
            }
        }

        public Wave()
        {
        }

        public Wave(int waveNumber, int numOfEnemies, Map map, Texture2D enemyTexture)
        {
            this.WaveNumber = waveNumber;
            this.NumOfEnemies = numOfEnemies;

            this.Map = map;
            this.EnemyTexture = enemyTexture;
        }

        private void AddEnemy()
        {
            Random rand = new Random();
            int way;
            Enemy enemy;
            way = rand.Next(0, 6);

            switch (way)
            {
                case 0:
                    enemy = new Enemy(enemyTexture,map.Waypoints0.Peek(), 50, 10, 1.0f);
                    enemy.SetWaypoints(map.Waypoints0);
                    enemies.Add(enemy);
                    break;
                case 1:
                    enemy = new Enemy(enemyTexture,map.Waypoints1.Peek(), 50, 10, 1.0f);
                    enemy.SetWaypoints(map.Waypoints1);
                    enemies.Add(enemy);
                    break;
                case 2:
                    enemy = new Enemy(enemyTexture,map.Waypoints2.Peek(), 50, 10, 1.0f);
                    enemy.SetWaypoints(map.Waypoints2);
                    enemies.Add(enemy);
                    break;
                case 3:
                    enemy = new Enemy(enemyTexture,map.Waypoints6.Peek(), 50, 10, 1.0f);
                    enemy.SetWaypoints(map.Waypoints6);
                    enemies.Add(enemy);
                    break;
                case 4:
                    enemy = new Enemy(enemyTexture,map.Waypoints7.Peek(), 50, 10, 1.0f);
                    enemy.SetWaypoints(map.Waypoints7);
                    enemies.Add(enemy);
                    break;
                case 5:
                    enemy = new Enemy(enemyTexture,map.Waypoints8.Peek(), 50, 10, 1.0f);
                    enemy.SetWaypoints(map.Waypoints8);
                    break;
            }                        
            spawnTimer = 0;
            enemiesSpawned++;
        }

        public void Start()
        {
            spawningEnemies = true;
        }

        public void Update(GameTime gameTime, Player player)
        {
            if (enemiesSpawned == numOfEnemies)
                spawningEnemies = false; //Se han creado suficientes enemigos

            if (spawningEnemies)
            {
                spawnTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (spawnTimer > 2)
                    AddEnemy(); //Indica que debe agregar un enemigo
            }

            for (int i = 0; i < enemies.Count; i++)
            {
                Enemy enemy = enemies[i];
                enemy.Update(gameTime);
                if (enemy.IsDead)
                {
                    if (enemy.CurrentHealth > 0) //El enemigo llega al final
                    {
                        enemyAtEnd = true;
                    }
                    else 
                    {
                        player.Points = player.Points + enemy.BountyGiven;
                    }

                    enemies.Remove(enemy);
                    i--;                    
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Enemy enemy in enemies)
                enemy.Draw(spriteBatch);
        }
    }
}
